﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Web.Global;
using Portal.Web.Framework;
using Portal.Utilities;
using Portal.AssetTn.Model;
using System.IO;
using Portal.Core;

namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class AjaxAssetNationalProcess
    {
        public static string GetPlugInJavascript(string SiteId)
        {
            return
                "<script language=javascript>\r\n" +
                "   function ClientShowFormViewAssetNational(AssetNationalId)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('divMainForm').style.display='none';\r\n" +
                "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideGetViewForm(SiteId, AssetNationalId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                "       document.getElementById('divActionForm').style.display='block';\r\n" +
                "   }\r\n" +

                "   function ClientShowFormUpdateAssetNational(AssetNationalId)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('divMainForm').style.display='none';\r\n" +
                "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideGetUpdateForm(SiteId, AssetNationalId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                "       document.getElementById('divActionForm').style.display='block';\r\n" +
                "       document.getElementById('txtAssetNationalName').focus();\r\n" +
                "   }\r\n" +

                "   function ClientSaveUpdateAssetNational(AssetNationalId)\r\n" +
                "   {\r\n" +
                "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideCreateAssetNationalObject(SiteId, AssetNationalId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       OAssetNational = AjaxOut.RetObject;\r\n" +
                "       OAssetNational.AssetNationalName=document.getElementById('txtAssetNationalName').value;\r\n" +
                "       AjaxOut =  Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideSaveUpdateAssetNational(SiteId, AssetNationalId, OAssetNational).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       alert(AjaxOut.InfoMessage);\r\n" +
                "       ClientCloseActionForm();\r\n" +
                "   }\r\n" +


                "   function ClientShowFormAddAssetNational(AssetNationalId)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('divMainForm').style.display='none';\r\n" +
                "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideGetAddForm(SiteId).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       document.getElementById('divActionForm').innerHTML=AjaxOut.HtmlContent;\r\n" +
                "       document.getElementById('divActionForm').style.display='block';\r\n" +
                "       document.getElementById('txtAssetNationalName').focus();\r\n" +
                "   }\r\n" +


                "   function ClientSaveAddAssetNational(AssetNationalId)\r\n" +
                "   {\r\n" +
                "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideCreateAssetNationalObject(SiteId, null).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       OAssetNational = AjaxOut.RetObject;\r\n" +
                "       OAssetNational.AssetNationalName=document.getElementById('txtAssetNationalName').value;\r\n" +
                "       AjaxOut =  Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideSaveAddAssetNational(SiteId, OAssetNational).value;\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       alert(AjaxOut.InfoMessage);\r\n" +
                "       ClientCloseActionForm();\r\n" +
                "   }\r\n" +


                "   function ClientDeleteAssetNational(AssetNationalId)\r\n" +
                "   {\r\n" +
                "       if(confirm('Bạn đã chắc chắn xóa chưa?')==false)return;\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideDeleteAssetNational(SiteId, AssetNationalId).value;\r\n" +
                "       document.getElementById('divProcessing').innerHTML='';\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       RealClientJumpPage();\r\n" +
                "   }\r\n" +

                "   function ClientCloseActionForm()\r\n" +
                "   {\r\n" +
                "       document.getElementById('divMainForm').style.display='block';\r\n" +
                "       document.getElementById('divActionForm').style.display='none';\r\n" +
                "       ClientJumpPage(0);\r\n" +
                "   }\r\n" +

                "   function ClientJumpPage(CurrentPageIndex)\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       document.getElementById('txtPageIndex').value=CurrentPageIndex;\r\n" +
                "       document.getElementById('divProcessing').innerHTML='Đang đọc dữ liệu từ máy chủ...';\r\n" +
                "       setTimeout('RealClientJumpPage()',10);\r\n" +
                "   }\r\n" +

                "   function RealClientJumpPage()\r\n" +
                "   {\r\n" +
                "       SiteId='" + SiteId + "';\r\n" +
                "       PageIndex = document.getElementById('txtPageIndex').value;\r\n" +
                "       Keyword = document.getElementById('txtKeyword').value;\r\n" +
                "       AjaxOut = Portal.Gui.Web.WebParts.AssetTn.AjaxAssetNationalProcess.ServerSideReading(SiteId, Keyword, PageIndex).value;\r\n" +
                "       document.getElementById('divProcessing').innerHTML='';\r\n" +
                "       if(AjaxOut.Error)\r\n" +
                "       {\r\n" +
                "           alert(AjaxOut.InfoMessage);\r\n" +
                "           return;\r\n" +
                "       }\r\n" +
                "       document.getElementById('divAssetNationalContent').innerHTML=AjaxOut.HtmlContent;\r\n" +
                "   }\r\n" +
                "</script>\r\n";
        }


        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideReading(string SiteId, string Keyword, string PageIndex)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                AssetNationalCls[] AssetNational = new AssetNationalBll().ReadingAssetNational(SiteId, Keyword);

                string RetHtml =
                 //   "<div class=\"CssRecordFound\">Tìm thấy tổng số (" + AssetNational.Length.ToString("#,##0") + ") vị trí công việc theo điều kiện lọc</div>\r\n" +
                    "<div id=\"divProcessing\"></div>";

                if (AssetNational.Length == 0)
                {
                    RetHtml += "<div class=\"CssRecordFound\">Chưa có dữ liệu nào trong chuyên mục này!</div>";
                }
                else
                {
                    ReturnPaging oReturnPaging = PagingUtilities.GetPaging(AssetNational.Length, int.Parse(PageIndex), 25, 10, "ClientJumpPage");
                    RetHtml +=
                    oReturnPaging.PagingText +
                    "   <div class=\"tabcontent\">\r\n" +
                    "   <table>\r\n" +
                    "       <tr>" +
                    "           <th class=\"thIndex\">#</th>" +
                    "           <th class=\"thIcon\"></th>" +
                    "           <th>Dân tộc</th>" +
                    "           <th></th>" +
                    "       </tr>";
                    int StartIndex = oReturnPaging.StartIndex;
                    int EndIndex = oReturnPaging.EndIndex;
                    //int LevelIndex = 0;
                    for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                    {
                        string AssetNationalId = AssetNational[iIndex].AssetNationalId;
                        string AssetNationalName = AssetNational[iIndex].AssetNationalName;

                        string Ids = "Item" + iIndex;
                        string ButtonText = FunctionButton.GetButtonItems(new ButtonItem[]{
                            new DeleteButtonItem(SiteId, "delete","Xóa Dân tộc","javascript:ClientDeleteAssetNational('" + AssetNationalId + "');"),
                            new UpdateButtonItem(SiteId, "update","Sửa chữa Dân tộc","javascript:ClientShowFormUpdateAssetNational('" + AssetNationalId + "');"),
                        });
                        RetHtml += "<tr>" +
                                   "      <td class=\"tdIndex\">" + (iIndex + 1) + "</td>" +
                                   "      <td class=tdIcon><img class=\"image\" src=\"" + Environments.GetTemplateFolder(SiteId) + "/images/modules/small-AssetNational.png\"></td>\r\n" +
                                   "      <td><a href=\"javascript:ClientShowFormViewAssetNational('" + AssetNationalId + "');\">" + AssetNationalName + "</a></td>" +
                                   "      <td class=\"tdFunction\">\r\n" +
                                            ButtonText+
                                   "     </td>" +
                                   "</tr>";
                    }
                    RetHtml +=
                        "   </table>\r\n</div>\r\n";
                }
                oAjaxReturn.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }



        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideCreateAssetNationalObject(string SiteId, string AssetNationalId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                if (!string.IsNullOrEmpty(AssetNationalId))
                {
                    oAjaxReturn.RetObject = new AssetNationalBll().CreateAssetNationalModel(SiteId, AssetNationalId);
                }
                else
                {
                    oAjaxReturn.RetObject = AssetNationalParser.CreateInstance();
                }
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideSaveUpdateAssetNational(
            string SiteId,
            string AssetNationalId,
            AssetNationalCls OAssetNational)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                new AssetNationalBll().SaveAssetNational(SiteId, AssetNationalId, OAssetNational);
                oAjaxReturn.InfoMessage = "Cập nhật Dân tộc thành công";
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideSaveAddAssetNational(string SiteId, AssetNationalCls OAssetNational)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                new AssetNationalBll().AddAssetNational(SiteId, OAssetNational);
                oAjaxReturn.InfoMessage = "Thêm mới dân tộc thành công";
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideDeleteAssetNational(string SiteId, string AssetNationalId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                new AssetNationalBll().DeleteAssetNational(SiteId, AssetNationalId);
                oAjaxReturn.InfoMessage = "Xoá Dân tộc thành công";
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideGetUpdateForm(string SiteId, string AssetNationalId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                AssetNationalCls oAssetNational = new AssetNationalBll().CreateAssetNationalModel(SiteId, AssetNationalId);
                string Html =
                    "<table style=\"width:100%;\">\r\n" +
                    "   <tr>\r\n" +
                    "       <td style=\"width:30px;text-align:center;vertical-align:top;\"><img class=\"image\" src=\"" + Environments.GetTemplateFolder(SiteId) + "/images/modules/AssetNational-large.png\" style=\"margin:4px;width:80px;\"></td>\r\n" +
                    "       <td>\r\n" +
                    "           <div class=CssFormSaveUpdate>\r\n" +
                    "           <table>\r\n" +
                   
                    "           <tr>\r\n" +
                    "               <td class=Col1>Dân tộc:</td>\r\n" +
                    "               <td class=Col2><input type=textbox id=txtAssetNationalName value=\"" + oAssetNational.AssetNationalName + "\"></td>\r\n" +
                    "           </tr>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col1></td>\r\n" +
                    "               <td class=Col2><input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveUpdateAssetNational('" + AssetNationalId + "');\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientCloseActionForm('" + AssetNationalId + "');\"></td>\r\n" +
                    "           </tr>\r\n" +
                    "       </table>\r\n" +
                    "       </div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n";
                oAjaxReturn.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }




        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideGetAddForm(string SiteId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);

                string Html =
                    "<table style=\"width:100%;\">\r\n" +
                    "   <tr>\r\n" +
                    "       <td style=\"width:30px;text-align:center;vertical-align:top;\"><img class=\"image\" src=\"" + Environments.GetTemplateFolder(SiteId) + "/images/modules/AssetNational-large.png\" style=\"margin:4px;width:80px;\"></td>\r\n" +
                    "       <td>\r\n" +
                    "       <div class=CssFormSaveUpdate>\r\n" +
                    "       <table>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col1>Dân tộc:</td>\r\n" +
                    "               <td class=Col2><input type=textbox id=txtAssetNationalName></td>\r\n" +
                    "           </tr>\r\n" +
                    "           <tr>\r\n" +
                    "               <td class=Col1></td>\r\n" +
                    "               <td class=Col2><input type=button class=button value=\"Ghi lại\" onclick=\"javascript:ClientSaveAddAssetNational();\"><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientCloseActionForm();\"></td>\r\n" +
                    "           </tr>\r\n" +
                    "       </table>\r\n" +
                    "   </div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n";
                oAjaxReturn.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }



        [AjaxPro.AjaxMethod]
        public static AjaxReturn ServerSideGetViewForm(string SiteId, string AssetNationalId)
        {
            AjaxReturn oAjaxReturn = new AjaxReturn();
            try
            {
                Environments.CheckSessionTimeOut(true);
                AssetNationalCls oAssetNational = new AssetNationalBll().CreateAssetNationalModel(SiteId, AssetNationalId);
                string Html =
                    "<table style=\"width:100%;\">\r\n" +
                    "   <tr>\r\n" +
                    "       <td style=\"width:30px;text-align:center;vertical-align:top;\"><img class=\"image\" src=\"" + Environments.GetTemplateFolder(SiteId) + "/images/modules/AssetNational-large.png\" style=\"margin:4px;width:80px;\"></td>\r\n" +
                    "       <td>\r\n" +
                    "<div class=CssFormSaveUpdate>\r\n" +
                    " <table>\r\n" +
                    "       <tr>\r\n" +
                    "           <td class=Col1>Dân tộc:</td>\r\n" +
                    "           <td class=Col2>" + oAssetNational.AssetNationalName + "</td>\r\n" +
                    "       </tr>\r\n" +

                     "       <tr>\r\n" +
                    "           <td class=Col1></td>\r\n" +
                    "           <td class=Col2><input type=button class=button value=\"Quay lại\" onclick=\"javascript:ClientCloseActionForm('" + AssetNationalId + "');\"></td>\r\n" +
                    "       </tr>\r\n" +
                    "</table>\r\n" +
                    "</div>\r\n"+
                    "       </td>\r\n" +
                    "   </tr>\r\n" +
                    "</table>\r\n";
                oAjaxReturn.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                oAjaxReturn.Error = true;
                oAjaxReturn.InfoMessage = ex.Message.ToString();
            }
            return oAjaxReturn;
        }

    }
}
